using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DABL;

public partial class Control_Questionnaires : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            txtStartDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtEndDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            ddlAccounts.DataTextField = "AccountName";
            ddlAccounts.DataValueField = "AccountID";
            
            
            


            ddlAccounts.DataSource = QuestionnaireController.UserAccountsGet(Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());
            ddlAccounts.DataBind();

            
            
            if(Roles.IsUserInRole("admin"))
            {
                gViewQuestionnaires.DataSource = QuestionnaireController.QuestionnairesGet();
                gViewQuestionnaires.DataBind();
            }
            else
            {
                string userAccount = string.Empty;
                IDataReader dataReader = QuestionnaireController.UserAccountsGet(Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());
                while(dataReader.Read())
                {
                    userAccount = Convert.ToString(dataReader.GetInt32(0)) + "," + userAccount;
                }
                if(userAccount.Length > 0)
                    userAccount = userAccount.Remove(userAccount.Length - 1, 1);
                //Response.Write(userAccount);
                gViewQuestionnaires.DataSource = QuestionnaireController.qst_QuestionnairesGetByAccountID(userAccount);
                gViewQuestionnaires.DataBind();

            }
        }

    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        try
        {
            Questionnaire dsQuestionnaire = new Questionnaire();
            Questionnaire.qst_QuestionnairesRow QuestionnairesRow = (Questionnaire.qst_QuestionnairesRow)dsQuestionnaire.qst_Questionnaires.NewRow();
            QuestionnairesRow.ArchiveText = txtArchiveText.Text;
            QuestionnairesRow.LeadingText = txtLeadingText.Text;
            QuestionnairesRow.StartDate = Convert.ToDateTime(txtStartDate.Text);
            QuestionnairesRow.EndDate = Convert.ToDateTime(txtEndDate.Text);
            QuestionnairesRow.QuestionnaireName = txtQuestionnaire.Text;
            QuestionnairesRow.AccountID_FK =Convert.ToInt32(  ddlAccounts.SelectedValue);
            string QuesionnaireID = QuestionnaireController.QuestionnairesAdd(QuestionnairesRow);
            if(QuesionnaireID.Length > 0)
            {
                Response.Redirect("EditQuestionnaire.aspx?ID=" + QuesionnaireID);
            }
        }
        catch(System.FormatException ex)
        {
            Response.Write( ex.Message);
        }
    }
}
